/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.bff.designtime.generator.metadatacompile;

import com.inspur.edp.bef.bizentity.GspBizEntityObject;
import com.inspur.edp.bef.bizentity.GspBusinessEntity;
import com.inspur.edp.cef.designtime.api.IGspCommonField;
import com.inspur.edp.cef.designtime.api.element.GspAssociation;
import com.inspur.edp.cef.designtime.api.element.GspElementObjectType;
import com.inspur.edp.cef.designtime.api.element.GspEnumValue;
import com.inspur.edp.das.commonmodel.IGspCommonElement;
import com.inspur.edp.das.commonmodel.IGspCommonObject;
import com.inspur.edp.formserver.viewmodel.GspViewModelElement;
import com.inspur.edp.jittojava.context.JitContext;

import java.util.HashMap;

public
class VmElementCoincidenceCheck extends VmObjectCoincidenceCheck {

  protected IGspCommonField vmElement;
  protected GspBusinessEntity businessEntity;
  GspBizEntityObject bizEntityObject;
  protected IGspCommonField beElement;

  public VmElementCoincidenceCheck(JitContext context, IGspCommonObject viewObject,
      HashMap<String, GspBusinessEntity> relatedBizEntityDic,IGspCommonField vmElement,GspBusinessEntity businessEntity,GspBizEntityObject bizEntityObject,
      IGspCommonField beElement) {
    super(context, viewObject, relatedBizEntityDic);
    this.relatedBizEntityDic = relatedBizEntityDic;
    this.vmElement = vmElement;
    this.businessEntity =businessEntity;
    this.bizEntityObject = bizEntityObject;
    this.beElement = beElement;
  }

  //比较ViewModel字段(标签、数据类型)
  public void checkElement() {
//    if (!vmElement.getLabelID().equals(beElement.getLabelID())) {
//      this.exceptionMessage
//          .append(CheckCoincidenceException.throwFieldLabelIDException(viewModel,viewModelObject,vmElement,businessEntity,bizEntityObject, beElement));
//      return;
//    }
    if (!vmElement.getObjectType().equals(beElement.getObjectType())) {
      this.exceptionMessage
          .append(CheckCoincidenceException.throwObjectTypeException(viewModel,viewModelObject,vmElement,businessEntity,bizEntityObject, beElement));
      return;
    }
    if (!vmElement.getMDataType().equals(beElement.getMDataType())) {
      this.exceptionMessage
          .append(CheckCoincidenceException.throwDataTypeException(viewModel,viewModelObject,vmElement,businessEntity,bizEntityObject, beElement));
      return;
    }
    if (vmElement.getIsUdt() != beElement.getIsUdt()) {
        this.exceptionMessage.append(CheckCoincidenceException.throwFieldUdtException(viewModel,viewModelObject,vmElement,businessEntity,bizEntityObject, beElement));
        return;
    }
    if (vmElement.getHasAssociation()) {
      checkAssociation();
    }
    if (vmElement.getObjectType().equals(GspElementObjectType.Enum)) {
      checkEnumValues();
    }
  }


  protected void checkEnumValues() {
    Boolean vmIndexFound;
    for (GspEnumValue vmItem : vmElement.getContainEnumValues()) {
      vmIndexFound = false;
      for (GspEnumValue beItem : beElement.getContainEnumValues()) {
        if (vmItem.getIndex() != beItem.getIndex()) {
          continue;
        }
        if (vmItem.getValue().equals(beItem.getValue())) {
          vmIndexFound = true;
          break;
        }
        this.exceptionMessage
            .append(CheckCoincidenceException.throwEnumValueException(viewModel,viewModelObject,vmElement,businessEntity,bizEntityObject, beElement,vmItem, beItem));
        //退出循环，查找下一vmItem
      }
      if (vmIndexFound == false) {
        this.exceptionMessage.append(CheckCoincidenceException.throwFieldAssociationException(viewModel,viewModelObject,vmElement,businessEntity,bizEntityObject, beElement,"枚举值没找到，请检查！序号为： "+vmItem.getIndex()));
      }
    }
  }

  protected void checkAssociation() {
    if (!beElement.getHasAssociation()) {
      this.exceptionMessage.append(CheckCoincidenceException.throwFieldAssociationException(viewModel,viewModelObject,vmElement,businessEntity,bizEntityObject, beElement,"关联字段对应的BE上字段不是关联类型。"));
      return;
    }
    for (GspAssociation vmAssoField : vmElement.getChildAssociations()) {
      for (IGspCommonField vmBringOutField : vmAssoField.getRefElementCollection()) {
        if (vmBringOutField.getIsVirtual()) {
          continue;
        }
        if (!(vmBringOutField instanceof GspViewModelElement)) {
          this.exceptionMessage.append(CheckCoincidenceException.throwFieldAssociationException(viewModel,viewModelObject,vmElement,businessEntity,bizEntityObject, beElement,"关联带出字段类型异常。"));
          continue;
        }
        checkMappingElement((GspViewModelElement) vmBringOutField);

      }
    }
  }
}
